package com.dys.exception;

import com.alibaba.fastjson.JSON;
import com.dys.common.ResponseResult;
import com.dys.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 疾风
 * @ClassName: AccessDeniedHandlerImpl
 * @Description: 自定义授权过程中出现的异常处理
 * @CreateDate: 2024/12/17
 * @Version: v1.0.0
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResponseResult<Object> responseResult = new ResponseResult<>(HttpStatus.FORBIDDEN.value(), "权限不足,禁止访问");
        String jsonString = JSON.toJSONString(responseResult);
        WebUtils.renderString(response, jsonString);
    }
}
